home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / MoofWars / MoofEncoder / ICL8Encode.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  3.6 KB  |  124 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ICL8Encode.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by:  Timothy Carroll    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/1/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 4/2/97        Timothy Carroll    The icon and compiled sprite could be double
  21.                                             disposed before. Spotlight noticed this one in the
  22.                                             PICTEncode, but the same bug was in this code.  Now
  23.                                             I NULL out the variables after I dispose of them the
  24.                                             first time.
  25.                 2/24/97        Timothy Carroll    Now explicitly include main.h
  26.                 8/15/96        Timothy Carroll Initial Release
  27.                 
  28.  
  29. */
  30. #include <Resources.h>
  31. #include "Main.h"
  32. #include "TGraphic.h"
  33. #include "ICL8Encode.h"
  34.  
  35. OSStatus ICL8Encode (short inputFileResNum, short outputFileResNum)
  36. {
  37.     OSStatus        theErr;
  38.  
  39.     UInt16            numIcons, loop;
  40.  
  41.     // we pass these to GetResInfo so that we can get the actual resource ID.
  42.     SInt16            resID;
  43.     ResType            resType;
  44.     Str255            resName;
  45.  
  46.     // Holds the last value on the resource chain since we change the top resource a lot
  47.     SInt16            saveResNum;
  48.  
  49.     // Temporarily holds the resource so we can get information on it
  50.     Handle             icon = NULL;
  51.     TGraphic         *compiled = NULL;
  52.         
  53.     saveResNum = CurResFile();
  54.  
  55.     UseResFile (inputFileResNum);
  56.  
  57.  
  58.     // First thing is to copy the color table resource to the output.
  59.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  60.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  61.  
  62.  
  63.     // get the color table
  64.     gAppColorTable = GetCTable( kAppColorTableResID );
  65.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  66.  
  67.     // determine the number of icl8 resources
  68.     numIcons = Count1Resources( 'icl8' );
  69.  
  70.     
  71.     // get each one,
  72.     for( loop = 1; loop <= numIcons; loop++ )
  73.     {
  74.         // load the icon
  75.         UseResFile (inputFileResNum);
  76.                     
  77.         // we'll get the icon first so that we can get the information out of it.
  78.         icon = Get1IndResource( 'icl8', loop );
  79.         theErr = ResError();
  80.         FAIL_NIL (icon, "\pFailed to load the icon")
  81.         FAIL_OSERR (theErr, "\pFailed to load the icon")
  82.             
  83.         // determine its id and name
  84.         GetResInfo( icon, &resID, &resType, resName );
  85.         theErr = ResError();
  86.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  87.         
  88.         ReleaseResource (icon);
  89.         icon = NULL; // So that the error code doesn't double dispose of it.
  90.         
  91.         // Okay, we know the icl8's resID, build the compiled graphic and write it to the output file.
  92.         compiled = TGraphic::NewGraphic (resID);
  93.         FAIL_NIL (compiled, "\pFailed to compile the TGraphic")
  94.         
  95.         UseResFile (outputFileResNum);
  96.         
  97.         compiled->WriteToGraphicResource();
  98.         compiled->WriteToPICTResource();
  99.         compiled->DisposeReference();
  100.         compiled = NULL; // so that the error code doesn't double dispose of it.
  101.  
  102.     }
  103.     // restore the previous port and device
  104.     
  105.     goto cleanup;
  106.     
  107. error:
  108.     
  109.     if (theErr == noErr)
  110.         theErr = paramErr;
  111.  
  112.     if (icon)
  113.         ReleaseResource (icon);
  114.     if (compiled)
  115.         compiled->DisposeReference();
  116. cleanup:
  117.     
  118.     UseResFile (saveResNum);
  119.     
  120.     if (gAppColorTable != NULL)
  121.         DisposeCTable (gAppColorTable);
  122.     gAppColorTable = NULL;
  123.     return theErr;
  124. }